import { Space, Table, Tag } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import React from 'react';

//声明一个接口  定义单条数据对象的结构
interface DataType {
  key: string;
  name: string;
  age: number;
  address: string;
  tags: string[];
}

//定义表格的结构
const columns: ColumnsType<DataType> = [
  {
    title: '姓名',
    dataIndex: 'name', //dataIndex 数据索引
    //render 渲染
    render: text => <button>{text}</button>,
  },
  {
    title: '年龄',
    dataIndex: 'age',
    render: (v) => {
      return v - 3;
    }
  },
  {
    title: '地址',
    dataIndex: 'address',
  },
  {
    title: '标签',
    key: 'tags',
    dataIndex: 'tags',
    // render 回调函数的第一个参数是 dataIndex 属性在数据对象中的属性值
    // 第二个参数就是当前的完整的数据对象
    render: (_, record) => {
     
      return <>
        {
          record.tags.map((item, index) => {
            return <Tag key={index}>{item}</Tag>
          })
        }
      </>
    },
  },
  {
    title: '操作',
    render: (_, record) => (
      <Space size="middle">
        <button>修改</button>
        <button onClick={() => {
          alert(record.key);
        }}>删除</button>
      </Space>
    ),
  },
];

//准备数据
const data: DataType[] = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park',
    tags: ['nice', 'developer'],
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    address: 'London No. 1 Lake Park',
    tags: ['loser'],
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    address: 'Sidney No. 1 Lake Park',
    tags: ['cool', 'teacher'],
  },
];

//使用 Table 组件渲染数据
const TableCom: React.FC = () => <Table columns={columns} dataSource={data} />;

export default TableCom;